<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>优惠券管理</el-breadcrumb-item>
        <el-breadcrumb-item>优惠券列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-search">
      <el-row>
        <el-input
          style="width: 200px;margin-right: 10px"
          placeholder="请输入优惠券信息查询"
          v-model="keyword"
          clearable
          @clear="resetData"
        >
        </el-input>
        <el-button type="primary" @click="resetData" >搜索</el-button>
      </el-row>
    </div>
    <div class="table-content">
      <div class="table-bar">
        <el-button type="primary" @click="handleAdd">新增优惠券</el-button>
        <el-button type="danger" @click="handleBatchDel">批量删除</el-button>
      </div>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="coupons"
        tooltip-effect="dark"
        style="width: 100%"
        @select="selectedRow"
        @select-all="selectedAll"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="sort" label="排序" width="60"></el-table-column>
        <el-table-column prop="num" label="数量" width="60"></el-table-column>
        <el-table-column prop="has_num" label="未领取" width="70"></el-table-column>
        <el-table-column prop="name" label="名称" width="auto"></el-table-column>
        <el-table-column prop="type" label="优惠券类型" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.type == 1">普通</p>
            <p v-else-if="scope.row.type == 2">上新</p>
            <p v-else-if="scope.row.type == 3">爆款</p>
            <p v-else>优选</p>
          </template>
        </el-table-column>
        <el-table-column prop="condition" label="使用条件" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.condition > 0">{{scope.row.condition}}</p>
            <p v-else>不限制</p>
          </template>
        </el-table-column>
        <el-table-column prop="discount_type" label="优惠方式" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.discount_type == 1">立减</p>
            <p v-else-if="scope.row.discount_type == 2">打折</p>
            <p v-else>返利</p>
          </template>
        </el-table-column>
        <el-table-column prop="discount_money" label="优惠金额/折扣" width="auto">
          <template slot-scope="scope">
            <p v-if="scope.row.discount_type == 2">{{(scope.row.discount_money * 10)}}折</p>
            <p v-else >{{scope.row.discount_money}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" type="success">已上架</el-tag>
            <el-tag v-else type="warning">未上架</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sale_type" label="优惠类型" width="100">
          <template slot-scope="scope">
            <p v-if="scope.row.sale_type == 1">免费</p>
            <p v-else-if="scope.row.sale_type == 2">收费</p>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建日期" width="126">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ $moment(scope.row.create_time * 1000).format("YYYY-MM-DD") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin-right: 5px"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(scope.index,scope.row)"
            >
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        coupons: [],
        multipleSelection: [],
      }
    },
    mounted() {
      this.queryList();
    },
    methods: {
      handleAdd() {
        this.$router.push({name: 'addCoupon'})
      },
      handleEdit(index, row) {
        const id = row.id;
        this.$router.push({name: 'editCoupon', query: {id}});
      },
      handleDelete(index, row) {
        this.toDelete(row.id);
      },
      handleBatchDel() {
        if (this.multipleSelection.length < 1) {
          this.$message.error("请先勾选需要删除的优惠券！");
          return;
        }
        this.$confirm('确定要执行批量删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.toDelete(this.multipleSelection.join(","))
        }).catch(() => {
          console.log('放弃删除')
        });

      },
      selectedRow(selection, row) {
        this.multipleSelection = [];
        const _ths = this;
        selection.map(function (item) {
          _ths.multipleSelection.push(item.id);
        })
      },
      selectedAll(rows) {
        if (rows.length === 0) {
          this.multipleSelection = [];
        } else {
          const _ths = this;
          rows.map(function (item) {
            if (!_ths.multipleSelection.includes(item.id)) {
              _ths.multipleSelection.push(item.id);
            }
          })
        }
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.page = 1;
        this.queryList()
      },
      handleCurrentChange(val) {
        this.page = val;
        this.queryList();
      },
      queryList() {
        this.$http.get('/api/root/CouponShop/couponLst', {
          page: this.page,
          pagesize: this.pageSize,
          keyword: this.keyword
        }).then(rest => {
          if (rest.code == 1) {
            this.coupons = rest.data.data;
            this.total = rest.data.count;
          }
        })
      },
      toDelete(id_lst) {
        this.$http.get('/api/root/CouponShop/couponDel', {
          id_lst: id_lst,
        }).then(rest => {
          if (rest.code == 1) {
            this.queryList();
            this.multipleSelection = [];
            this.$message.success('删除成功');
          } else {
            this.$message.error("删除失败");
          }
        })
      }
      ,
      resetData(){
        this.page = 1;
        this.queryList();
      }
    }
  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
